	
	
prog define itt_het 


*** Run regressions
	eststo clear

	// Run regressions in loop 
	local j=1 
	di "Heterogeneity dimensions: ${het}"
	foreach h of global het {
		
		ta `h'
		local cats=`r(r)'
		
		local last=$p-1 // Last category on page 1 
		
		di "No. of categories: `cats', last cat. on page 1, `last'"
		
	foreach o of global outcome {
		
		// Run regression and margins command 
		di "Running regression for outcome: `o', heterogeneity dimension: `h'"
		*areg `o' i.Treat##i.`h' ${ldv_`o'_`h'} ${controls_`h'} missing_`o'_2021 ${se_`h'}
		areg `o' i.Treat##i.`h' `o'_2021 $controls_balance missing_`o'_2021 i.wave ${se_`h'}
		eststo m_`o': margins `h', dydx(1.Treat) post coeflegend 
	
		if inlist("`h'", "Old_Employee") {
		areg `o' i.Treat##i.`h' `o'_2021 educ_tert_base  i.ag_ano missing_`o'_2021 i.wave ${se_`h'}
		eststo m_`o': margins `h', dydx(1.Treat) post coeflegend 
		}	
		
		if inlist("`h'", "Tertiary_Educ") {
		areg `o' i.Treat##i.`h' `o'_2021 i.status  i.ag_ano missing_`o'_2021 i.wave ${se_`h'}
		eststo m_`o': margins `h', dydx(1.Treat) post coeflegend 
		}	
		
		estimates table m*


		* N 
		di "Save N and control group means in macro to include in table" 
		sum `o' if e(sample)
		local ${tbl}_n`j'=	r(N)
			
		* add to estimates 
		estadd 	local 		N_`j' `${tbl}_n`j'':m_`o'
		
		// Now for categories
		forv i=1/`cats' {
		di "sum `o' if `h'==`i' & e(sample)"
			sum `o' if `h'==`i' & e(sample)
			local ${tbl}_n`j'_`i'=r(N)

			sum `o' if Treat==0 & `h'==`i' & e(sample)
			local ${tbl}_cm`j'_`i'=round(r(mean),.001)
			
			* add to estimates 
			estadd 	local 		N_`i'_`j' `${tbl}_n`j'_`i'':m_`o'
			local 	cm_`i' 	= 	string(`${tbl}_cm`j'_`i'', "%9.3f")
			estadd local 		cm_`i'_`j' "`cm_`i''":m_`o'
			
			di "Control group mean: `${tbl}_cm`j'_`i''"
			di "Control group mean: `cm_`i''"
			
			
			}
			

	
	
	*** Significant difference between treatment effects? 
	
		*** Case of 4 categories
		if `cats'==4 {
		di "4 Categories: test for differences in treatment effects"

		** Conduct tests 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.Treat:2.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 2: `${tbl}_p`j'_12'"
		
		// 3 vs. 1 
		test _b[1.Treat:3.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 3: `${tbl}_p`j'_13'"
		
		// 4 vs. 1 
		test _b[1.Treat:4.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_14=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 4: `${tbl}_p`j'_14'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.Treat:2.`h'] = _b[1.Treat:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 3: `${tbl}_p`j'_23'"
		
		// 2 vs. 4
		test _b[1.Treat:2.`h'] = _b[1.Treat:4.`h'] 
		local ${tbl}_p`j'_24=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 4: `${tbl}_p`j'_24'"
		
		// 3 vs. 4 
		test _b[1.Treat:3.`h'] = _b[1.Treat:4.`h'] 
		local ${tbl}_p`j'_34=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 3 and 4: `${tbl}_p`j'_34'"
		

		** Add interaction to stored estimates 
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_14 = string(`${tbl}_p`j'_14', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")
		local interaction_24 = string(`${tbl}_p`j'_24', "%9.3f")
		local interaction_34 = string(`${tbl}_p`j'_34', "%9.3f")

		estadd local interaction_12_`j' `interaction_12':m_`o'
		estadd local interaction_13_`j' `interaction_13':m_`o'
		estadd local interaction_14_`j' `interaction_14':m_`o'
		estadd local interaction_23_`j' `interaction_23':m_`o'
		estadd local interaction_24_`j' `interaction_24':m_`o'
		estadd local interaction_34_`j' `interaction_34':m_`o'

		estimates table m*
		}
	
	*** Case of 3 categories
	if `cats'==3 {
		di "3 Categories: test for differences in treatment effects"
	*** Significant difference between treatment effects? 
	
		** Conduct tests 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.Treat:2.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 2: `${tbl}_p`j'_12'"
		
		
		test _b[1.Treat:3.`h'] =_b[1.Treat:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 3: `${tbl}_p`j'_13'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.Treat:2.`h'] = _b[1.Treat:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 3: `${tbl}_p`j'_23'"
		
		
		** Add interaction to stored estimates 
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")

		
		estadd local interaction_12_`j' `interaction_12':m_`o'
		estadd local interaction_13_`j' `interaction_13':m_`o'
		estadd local interaction_23_`j' `interaction_23':m_`o'

		
	estimates table m*

	}
	
	*** Case of 2 categories 
	if `cats'==2 {
		di "2 categories: test for differences in treatment effects"
		// 2 vs. 1 coefficients of the interaction term
		test _b[1.Treat:1.`h'] = _b[1.Treat:2.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 2: `${tbl}_p`j'_12'"

		** Add interaction to stored estimates 
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		
		estadd local interaction_12_`j' `interaction_12':m_`o'

	}
	

	}

	
**** Export results 
*** Options for all tables 
	local gops b(%9.3f) se starlevels(${significance}) nolines nonumbers nomtitles label nonotes booktabs frag noobs nobaselevels sfmt(%9.2f) 
	
	local postheadA "\midrule \multicolumn{@span}{l}{\textbf{Panel ${lbl_`h'}}}  \\ \midrule \addlinespace "
	local postfootA " [4pt] \cmidrule(lr){2-5}"
	
	local postheadB ""
	local postfootB " [4pt] \cmidrule(lr){2-5} "

	local postheadC ""
	local postfootC ""
	
	
	* Page 1 
	cd "$results\01_tables"
	// Create table newly for first heterogeneity dimension 
	if `j'==1 { 
		
		* Cat1 
		esttab m* using "${tbl}.tex", `gops' ///
		keep(1.Treat:1.`h' ) coeflabels(1.Treat:1.`h' "\textbf{${lbl_`h'_1}}" ) ///
		 scalars("cm_1_`j' Mean" "N_1_`j' \(N\)" )  ///
		posthead(`postheadA') postfoot(`postfootA') replace ///
		nogaps 	substitute($substitute)

	}
	
	// Append for second category and all that fit onto one page 
	if inrange(`j',2,`last') { 
		
		* Cat1 
		esttab m* using "${tbl}.tex", `gops' ///
		keep(1.Treat:1.`h' ) coeflabels(1.Treat:1.`h' "\textbf{${lbl_`h'_1}}" ) ///
		 scalars("cm_1_`j' Mean" "N_1_`j' \(N\)" )  ///
		posthead(`postheadA') postfoot(`postfootA') append  ///
		 nogaps substitute($substitute)

	}	
	

	// Append TEs for page 1 
		
		
		di "J=`j', last=`last', cats=`cats'"
		* 2 cats  
	if inrange(`j',1, `last') & `cats'==2  { 
			di "No. of categories: 2"
				esttab m* using "${tbl}.tex", `gops' ///
				keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
				 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
				postfoot(`postfootA') append  ///
				 nogaps substitute($substitute)
				
				esttab m* using "${tbl}.tex", `gops'  ///
				drop(*) scalars( "interaction_12_`j' P-val.\ for diff.\ in coeff.\")    ///
				posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
				substitute("\_" "_")
		}
		
		
		* 3 cats 
		if inrange(`j',1, `last') & `cats'==3 {
			* TE 2 
			esttab m* using "${tbl}.tex", `gops' ///
			keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
			 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			* TE 3 
			esttab m* using "${tbl}.tex", `gops' ///
			keep(1.Treat:3.`h') coeflabels(1.Treat:3.`h' "\textbf{${lbl_`h'_3}}" ) ///
			 scalars("cm_3_`j' Mean" "N_3_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)
			
			esttab m* using "${tbl}.tex", `gops'  ///
			drop(*) scalars("interaction_12_`j' P-val.\ for diff.\ in coeff.\ 1 and 2" "interaction_13_`j' P-val.\ for diff.\ in coeff.\ 1 and 3" "interaction_23_`j' P-val.\ for diff.\ in coeff.\ 2 and 3")    ///
			posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
			substitute("\_" "_")
			
		}
		
		* 4 cats 
		if inrange(`j',1, `last') & `cats'==4 {
			* TE 2 
			esttab m* using "${tbl}.tex", `gops' ///
			keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
			 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			* TE 3 
			esttab m* using "${tbl}.tex", `gops' ///
			keep(1.Treat:3.`h') coeflabels(1.Treat:3.`h' "\textbf{${lbl_`h'_3}}" ) ///
			 scalars("cm_3_`j' Mean" "N_3_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)
			
			* TE 4
			esttab m* using "${tbl}.tex", `gops' ///
			keep(1.Treat:4.`h') coeflabels(1.Treat:4.`h' "\textbf{${lbl_`h'_4}}" ) ///
			 scalars("cm_4_`j' Mean" "N_4_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			
			esttab m* using "${tbl}.tex", `gops'  ///
			drop(*) scalars("interaction_12_`j' P-val.\ for diff.\ in coeff.\ 1 and 2" "interaction_13_`j' P-val.\ for diff.\ in coeff.\ 1 and 3" "interaction_14_`j' \(P(ITT_1=ITT_4)\)" "interaction_23_`j' P-val.\ for diff.\ in coeff.\ 2 and 3" "interaction_24_`j' P-val.\ for diff.\ in coeff.\ 2 and 4" "interaction_34_`j' P-val.\ for diff.\ in coeff.\ 3 and 4")    ///
			posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
			substitute("\_" "_")
			
		}
		
		
		
	
	* Page 2 
	cd "$results\01_tables"
	// Create table newly for first heterogeneity dimension 
	if `j'==$p { 
		
		* Cat1 
		esttab m* using "${tbl}2.tex", `gops' ///
		keep(1.Treat:1.`h' ) coeflabels(1.Treat:1.`h' "\textbf{${lbl_`h'_1}}" ) ///
		 scalars("cm_1_`j' Mean" "N_1_`j' \(N\)" )  ///
		posthead(`postheadA') postfoot(`postfootA') replace  ///
	    nogaps 	substitute($substitute)

	}
	
	// Append for second and all that fit onto one page 
	if `j'>$p{ 
		
		* Cat1 
		esttab m* using "${tbl}2.tex", `gops' ///
		keep(1.Treat:1.`h' ) coeflabels(1.Treat:1.`h' "\textbf{${lbl_`h'_1}}" ) ///
		 scalars("cm_1_`j' Mean" "N_1_`j' \(N\)")  ///
		posthead(`postheadA') postfoot(`postfootA') append  ///
		 nogaps substitute($substitute)	

	}	
	

	// Append statistics for page 1 
	if `j'>=$p & `cats'==2 { 
		* Cat2 
		esttab m* using "${tbl}2.tex", `gops' ///
		keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
		 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
		postfoot(`postfootA') append  ///
		 nogaps substitute($substitute)
		
		esttab m* using "${tbl}2.tex", `gops'  ///
		drop(*) scalars( "interaction_12_`j' P-val.\ for diff.\ in coeff.\")    ///
		posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
		substitute("\_" "_")
		}
	
	
			* 3 cats 
		if `j'>=$p &  `cats'==3 {
			* TE 2 
			esttab m* using "${tbl}2.tex", `gops' ///
			keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
			 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			* TE 3 
			esttab m* using "${tbl}2.tex", `gops' ///
			keep(1.Treat:3.`h') coeflabels(1.Treat:3.`h' "\textbf{${lbl_`h'_3}}" ) ///
			 scalars("cm_3_`j' Mean" "N_3_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)
			
			esttab m* using "${tbl}2.tex", `gops'  ///
			drop(*) scalars("interaction_12_`j' P-val.\ for diff.\ in coeff.\ 1 and 2" "interaction_13_`j' P-val.\ for diff.\ in coeff.\ 1 and 3" "interaction_23_`j' P-val.\ for diff.\ in coeff. 2 and 3")    ///
			posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
			substitute("\_" "_")
			
		}
		
		* 4 cats 
		if `j'>=$p &  `cats'==4 {
			* TE 2 
			esttab m* using "${tbl}2.tex", `gops' ///
			keep(1.Treat:2.`h') coeflabels(1.Treat:2.`h' "\textbf{${lbl_`h'_2}}" ) ///
			 scalars("cm_2_`j' Mean" "N_2_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			* TE 3 
			esttab m* using "${tbl}2.tex", `gops' ///
			keep(1.Treat:3.`h') coeflabels(1.Treat:3.`h' "\textbf{${lbl_`h'_3}}" ) ///
			 scalars("cm_3_`j' Mean" "N_3_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)
			
			* TE 4
			esttab m* using "${tbl}2.tex", `gops' ///
			keep(1.Treat:4.`h') coeflabels(1.Treat:4.`h' "\textbf{${lbl_`h'_4}}" ) ///
			 scalars("cm_4_`j' Mean" "N_4_`j' \(N\)" ) ///
			postfoot(`postfootA') append  ///
			 nogaps substitute($substitute)

			
			esttab m* using "${tbl}2.tex", `gops'  ///
			drop(*) scalars("interaction_12_`j' P-val.\ for diff.\ in coeff.\ 1 and 2)" "interaction_13_`j' P-val.\ for diff.\ in coeff.\ 1 and 3" "interaction_14_`j' P-val.\ for diff.\ in coeff.\ 1 and 4" "interaction_23_`j' P-val.\ for diff.\ in coeff.\ 2 and 3" "interaction_24_`j' P-val.\ for diff.\ in coeff.\ 2 and 4" "interaction_34_`j' P-val.\ for diff.\ in coeff.\ 3 and 4")    ///
			posthead(`postheadB') postfoot(`postfootB')  append  nogaps ///
			substitute("\_" "_")
			
		}
	
	
	
	local ++j
	
	eststo clear 
	}
	

	
end

